package com.jandar.spider.utils;
//import com.cl.selenium.entity.Case;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author long
 * @date 2020/8/24
 */
public class SeleniumUtil {


    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "d://chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        driver.manage().window().maximize();

        driver.get("http://www.baidu.com");

        String title = driver.getTitle();
        System.out.println(title);

//        size(driver, 900, 900);
//        to(driver, "https://www.jianshu.com/p/20526e2ac3b1");
//        Thread.sleep(1000);
//        back(driver);
//        Thread.sleep(1000);
//        forward(driver);
//        fresh(driver);

        to(driver, "https://www.baidu.com");

        sendKeysById(driver, "kw", "java");
        clickById(driver, "su");
//        holdByText(driver,"设置");
        contextClickById(driver, "su");

        WebElement before = driver.findElement(By.linkText("设置"));
        WebElement after = driver.findElement(By.id("kw"));
//        drag(driver,before,after);

        iterator(driver, "//div/div/h3");
        Thread.sleep(10000);
        driver.close();


    }



//    public static String getResult(Chromedriver driver, Case aCase, String expectType) {
//        switch (expectType) {
//            case "textEqual":
//                if (findElement(driver, aCase.getCaseExpectXpath()).getText().equals(aCase.getCaseExpect())) {
//                    return SUCCESS;
//                }
//            case "valueEqual":
//                if (findElement(driver, aCase.getCaseExpectXpath()).getAttribute("value").equals(aCase.getCaseExpect())) {
//                    return SUCCESS;
//                }
//            case "exist":
//                if (findElement(driver, aCase.getCaseExpectXpath()) != null) {
//                    return SUCCESS;
//                }
//            default:
//                break;
//        }
//        return FAIL;
//    }

    public static WebDriver getDriver() {
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "d://chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        return driver;
    }

    public static WebElement findElement(WebDriver driver, String xpath) {
        try {
            return driver.findElement(By.xpath(xpath));
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static void size(WebDriver driver, int width, int height) {
        driver.manage().window().setSize(new Dimension(width, height));
    }

    public static void forward(WebDriver driver) {
        driver.navigate().forward();
    }

    public static void back(WebDriver driver) {
        driver.navigate().back();
    }

    public static void fresh(WebDriver driver) {
        driver.navigate().refresh();
    }

    public static void to(WebDriver driver, String url) {
        driver.navigate().to(url);
    }

    public static void sendKeysById(WebDriver driver, String id, String keys) {
        WebElement text = driver.findElement(By.id(id));
        text.sendKeys(keys);
    }

    public static void clickById(WebDriver driver, String id) {
        WebElement target = driver.findElement(By.id(id));
        target.click();
    }
    public static void clickByXpath(WebDriver driver, String xpath) {
        WebElement target = driver.findElement(By.xpath(xpath));
        target.click();
    }
    public static void clickByCSS(WebDriver driver, String CSS) {
        WebElement target = driver.findElement(By.cssSelector(CSS));
        target.click();
    }

    public static void holdByText(WebDriver driver, String text) {
        WebElement target = driver.findElement(By.linkText(text));
        Actions actions = new Actions(driver);
        actions.clickAndHold(target).perform();
    }

    public static void contextClickById(WebDriver driver, String id) {
        WebElement target = driver.findElement(By.id(id));
        Actions actions = new Actions(driver);
        actions.contextClick(target).perform();
    }

    public static void doubleClickById(WebDriver driver, String id) {
        WebElement target = driver.findElement(By.id(id));
        Actions actions = new Actions(driver);
        actions.doubleClick(target).perform();
    }

    public static void drag(WebDriver driver, WebElement before, WebElement after) {
        Actions actions = new Actions(driver);
        actions.dragAndDrop(before, after).perform();
    }

    public static void iterator(WebDriver driver, String xpath) {
        List<WebElement> list = driver.findElements(By.xpath(xpath));
        for (WebElement webElement : list) {
            System.out.println(webElement.getText());
        }
    }

    private static void KeyBoardOperation(WebDriver driver) throws InterruptedException {

        WebDriver driver5;
        driver5 = driver;

        WebElement input = driver5.findElement(By.id("kw"));

        //输入下准备数据
        input.sendKeys("Seleniumm");
        Thread.sleep(2000);

        //按下删除键,删除一个 "m"
        input.sendKeys(Keys.BACK_SPACE);
        Thread.sleep(2000);

        //输入空格键
        input.sendKeys(Keys.SPACE);
        input.sendKeys(" + Java");
        Thread.sleep(2000);

        //模拟 Ctrl + a ,全选
        input.sendKeys(Keys.CONTROL, "a");
        Thread.sleep(2000);

        //模拟 Ctrl + x ,剪切
        input.sendKeys(Keys.CONTROL, "x");
        Thread.sleep(2000);

        //模拟 Ctrl + v ,粘贴
        input.sendKeys(Keys.CONTROL, "v");
        Thread.sleep(2000);

        //模拟回车键
        input.sendKeys(Keys.ENTER);
        Thread.sleep(2000);

        driver5.quit();
    }
    public static void closeWindow(WebDriver driver) {

        try {
            String winHandleBefore = driver.getWindowHandle();//关闭当前窗口前，获取当前窗口句柄
            Set<String> winHandles = driver.getWindowHandles();//使用set集合获取所有窗口句柄

            driver.close();//关闭窗口

            //创建迭代器，迭代winHandles里的句柄
            //获取集合中的元素
            for (String win : winHandles) {//用it.hasNext()判断时候有下一个窗口,如果有就切换到下一个窗口
                if (!win.equals(winHandleBefore)) { //如果此窗口不是关闭前的窗口
                    driver.switchTo().window(win);//切换到新窗口
                    break;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();

        }

    }

}
